import os
import re


def get_modules(package="."):
    """
    获取包名下所有非__init__的模块名
    """
    modules = []
    files = os.listdir(package)

    for file in files:
        if not file.startswith("__"):
            name, ext = os.path.splitext(file)
            modules.append("." + name)

    return modules


def get_first_func_by_ann(cls_path, ann):
    ann_str = "@" + ann
    file = open(cls_path + ".py", "r", encoding="UTF-8")
    find_flag = False
    func_name = None

    # 逐行读取文件内容 匹配 aan 注解的方法名
    while True:
        line = file.readline()
        # 迭代到最后一行时退出循环
        if not line:
            break
        line = line.strip()
        if ann_str in line:
            find_flag = True
        if find_flag is True and "def" in line:
            p = r'.*def (.*)\('
            func_name = re.findall(p, line).pop()
            break
    # 关闭连接
    file.close()
    return func_name
